package com.jzr.bedside.service

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import com.blankj.utilcode.utils.LogUtils
import com.jzr.bedside.base.Constant
import com.jzr.bedside.ui.voip.VoipAudioRingingActivity
import com.jzr.bedside.ui.voip.VoipRingingActivity
import com.jzr.bedside.utils.CommonUtil
import com.jzr.bedside.utils.PreferUtil
import com.jzr.bedside.utils.rtc.AEvent
import com.jzr.bedside.utils.rtc.IEventListener
import com.jzr.bedside.utils.rtc.listener.XHLoginManagerListener
import com.jzr.bedside.utils.rtc.listener.XHVoipManagerListener
import com.jzr.bedside.utils.rtc.listener.XHVoipP2PManagerListener
import com.starrtc.starrtcsdk.api.XHClient
import com.starrtc.starrtcsdk.api.XHCustomConfig
import com.starrtc.starrtcsdk.apiInterface.IXHResultCallback


class KeepLiveService : Service(), IEventListener {

    private var isLogin = false

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        initSDK()
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        removeListener()
    }

    private fun initSDK() {
        initFree()
    }

    private fun initFree() {
        isLogin = XHClient.getInstance().isOnline
        if (!isLogin) {
            if (Constant.userId == "") {
//                Constant.userId = CommonUtil.getIP().toString()
                Constant.userId = PreferUtil.getInstance().bedName
                PreferUtil.getInstance().userId = Constant.userId
            }
            addListener()
            val customConfig =
                XHCustomConfig.getInstance(this)
            customConfig.chatroomServerUrl = Constant.CHATROOM_SERVER_URL
            customConfig.liveSrcServerUrl = Constant.LIVE_SRC_SERVER_URL
            customConfig.liveVdnServerUrl = Constant.LIVE_VDN_SERVER_URL
            customConfig.setImServerUrl(Constant.IM_SERVER_URL)
            customConfig.voipServerUrl = Constant.VOIP_SERVER_URL
            //customConfig.setLogEnable(false); //关闭SDK调试日志
//            customConfig.setDefConfigOpenGLESEnable(false);
            customConfig.setDefConfigCameraId(1);//设置默认摄像头方向  0后置  1前置
//            customConfig.setDefConfigVideoSize(XHConstants.XHCropTypeEnum.STAR_VIDEO_CONFIG_360BW_640BH_180SW_320SH);
//            customConfig.setLogDirPath(Environment.getExternalStorageDirectory().getPath()+"/starrtcLog");
//            customConfig.setDefConfigCamera2Enable(false);
//            StarCamera.setFrameBufferEnable(false);
            customConfig.initSDKForFree(
                Constant.userId,
                { errMsg, data ->
                },
                Handler()
            )
            XHClient.getInstance().voipManager
                .addListener(XHVoipManagerListener())
            XHClient.getInstance().voipP2PManager
                .addListener(XHVoipP2PManagerListener())
            XHClient.getInstance().loginManager
                .addListener(XHLoginManagerListener())
            XHClient.getInstance().loginManager
                .loginFree(object : IXHResultCallback {
                    override fun success(data: Any?) {
                        LogUtils.e("KeepLiveService", "loginSuccess")
                        isLogin = true
                    }
                    override fun failed(errMsg: String?) {
                        LogUtils.e("KeepLiveService", "loginFailed $errMsg")
                    }
                })
        }
    }

    private fun addListener() {
        AEvent.addListener(AEvent.AEVENT_LOGOUT, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_CALLING, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_REV_CALLING_AUDIO, this)
        AEvent.addListener(AEvent.AEVENT_VOIP_P2P_REV_CALLING, this)
        AEvent.addListener(AEvent.AEVENT_C2C_REV_MSG, this)
        AEvent.addListener(AEvent.AEVENT_REV_SYSTEM_MSG, this)
        AEvent.addListener(AEvent.AEVENT_GROUP_REV_MSG, this)
        AEvent.addListener(AEvent.AEVENT_USER_KICKED, this)
        AEvent.addListener(AEvent.AEVENT_CONN_DEATH, this)
    }

    private fun removeListener() {
        AEvent.removeListener(AEvent.AEVENT_LOGOUT, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_CALLING, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_REV_CALLING_AUDIO, this)
        AEvent.removeListener(AEvent.AEVENT_VOIP_P2P_REV_CALLING, this)
        AEvent.removeListener(AEvent.AEVENT_C2C_REV_MSG, this)
        AEvent.removeListener(AEvent.AEVENT_REV_SYSTEM_MSG, this)
        AEvent.removeListener(AEvent.AEVENT_GROUP_REV_MSG, this)
        AEvent.removeListener(AEvent.AEVENT_USER_KICKED, this)
        AEvent.removeListener(AEvent.AEVENT_CONN_DEATH, this)
    }

    override fun dispatchEvent(aEventID: String?, success: Boolean, eventObj: Any?) {
        when(aEventID){
            AEvent.AEVENT_VOIP_REV_CALLING->{
                val intent = Intent(this, VoipRingingActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("targetId", eventObj.toString())
                startActivity(intent)
            }

             AEvent.AEVENT_VOIP_REV_CALLING_AUDIO->{
                 val intent = Intent(this, VoipAudioRingingActivity::class.java)
                 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                 intent.putExtra("targetId", eventObj.toString())
                 startActivity(intent)
            }

        }
    }

}